package com.custom.action.core;

import com.custom.action.util.DbUtil;
import com.custom.action.core.annotations.DbLogic;
import com.custom.comm.utils.*;
import lombok.Getter;

import java.lang.reflect.Field;

/**
 * 逻辑删除注解的解析
 * @author XiaoBai
 * @since 2023/8/31 0031 22:03
 */
@Getter
public class DbLogicParserModel<T> extends DbFieldParserModel<T> {

    /**
     * 正常未逻辑删除的值
     */
    private final Object value;

    /**
     * 已逻辑删除的值
     */
    private final Object delValue;

    @Override
    public boolean isLogical() {
        return true;
    }

    public DbLogicParserModel(Field field, String table, String alias, boolean underlineToCamel, boolean existsDbField) {
        super(field, table, alias, underlineToCamel, existsDbField);
        DbLogic dbLogic = field.getAnnotation(DbLogic.class);
        this.value = Converts.transToObject(getType(), dbLogic.value());
        this.delValue = Converts.transToObject(getType(), dbLogic.delValue());
        super.setTypeHandlerClass(dbLogic.typeHandler());
    }

    /**
     * 逻辑删除条件
     */
    public String logicDelCondition() {
        if (JudgeUtil.isEmpty(value)
                || JudgeUtil.isEmpty(delValue)) {
            return Constants.EMPTY;
        }
        return DbUtil.formatLogicSql(getAlias(), getColumn(), delValue);
    }

    /**
     * 未逻辑删除条件
     */
    public String logicNotDelCondition() {
        if (JudgeUtil.isEmpty(value)
                || JudgeUtil.isEmpty(delValue)) {
            return Constants.EMPTY;
        }
        return DbUtil.formatLogicSql(getAlias(), getColumn(), value);
    }
}
